package com.happyscan138.makefun;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.happyscan138.makefun.BaseAdapter.my_message;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.myMessageAdapter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.happyscan138.makefun.base.share.access_key;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/6.
 */

public class myMessage extends AppCompatActivity implements View.OnClickListener {
    private ListView listViewMyMessage;
    private  List<my_message> listdata;
    private SharedPreferences preferences;
    private Button back;
    private my_message data;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_message);
        preferences=getSharedPreferences("UserInfo",MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        initView();
        getAdapterView();
    }
    public void initView(){
        listViewMyMessage=(ListView)findViewById(R.id.my_message_listview);
        back=(Button)findViewById(R.id.MyMessage_Back);
        back.setOnClickListener(this);
    }
    public void getAdapterView(){
        client=new AsyncHttpClient();
        params=new RequestParams();
        params.put("access_key",access_key);
        RequestHandle handle=client.post(Urls.MsgNotice,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                listdata=new ArrayList<>();
                String jsonStr=response.toString();
                log.e("messagedatajsonStr",jsonStr);
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject obj=jsonArray.getJSONObject(i);
                        data=new my_message();
                        data.head=base+obj.getString("img_path");
                        data.name=obj.getString("nick_name");
                        data.time=obj.getString("post_date");
                        data.user_id=obj.getString("user_id");
                        data.is_read=obj.getInt("is_read");
                        listdata.add(data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                log.e("messagedataSize",listdata.size()+"");
                listViewMyMessage.setAdapter(new myMessageAdapter(myMessage.this,listdata));
                listViewMyMessage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent=new Intent();
                        data=listdata.get(position);
                        intent.putExtra("user_id", data.user_id);
                        intent.setClass(myMessage.this,Chat.class);
                        startActivity(intent);
                    }
                });
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.MyMessage_Back:
                finish();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        getAdapterView();
    }
}
